
#include <p18f4550.h>
#include "Timer_Driver.h"


//volatile unsigned char Carga1s = 100 ;
extern volatile unsigned char Prueba;
extern void InterruptHandlerHigh (void);
extern volatile unsigned char Carga1s;



void initTimer0(void)
{
	/*
 T0CON: TIMER0 CONTROL REGISTER
	bit 7 TMR0ON: Timer0 On/Off Control bit 
		1 = Enables Timer0 
		0 = Stops Timer0
	bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit 
		1 = Timer0 is configured as an 8-bit timer/counter 
		0 = Timer0 is configured as a 16-bit timer/counter 
	bit 5 T0CS: Timer0 Clock Source Select bit 
		1 = Transition on T0CKI pin 
		0 = Internal instruction cycle clock (CLKO) 
	bit 4 T0SE: Timer0 Source Edge Select bit
		1 = Increment on high-to-low transition on T0CKI pin 
		0 = Increment on low-to-high transition on T0CKI pin 
	bit 3 PSA: Timer0 Prescaler Assignment bit 
		1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler. 
		0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
	bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits    
		111 = 1:256 Prescale value
		110 = 1:128 Prescale value
		101 = 1:64   Prescale value
		100 = 1:32   Prescale value
		011 = 1:16   Prescale value 
		010 = 1:8     Prescale value
		001 = 1:4     Prescale value
		000 = 1:2     Prescale value
	*/
	// con una escala de 125 se logra 1.024 segundos

	

	/*
INTCON: INTERRUPT CONTROL REGISTER
	bit 7 GIE/GIEH: Global Interrupt Enable bit
	  When IPEN = 0:
		1 = Enables all unmasked interrupts
		0 = Disables all interrupts
	  When IPEN = 1:
		1 = Enables all high priority interrupts
		0 = Disables all high priority interrupts
	bit 6 PEIE/GIEL: Peripheral Interrupt Enable bit
	  When IPEN = 0:
		1 = Enables all unmasked peripheral interrupts
		0 = Disables all peripheral interrupts
  	  When IPEN = 1:
		1 = Enables all low priority peripheral interrupts
		0 = Disables all low priority peripheral interrupts
	bit 5 TMR0IE: TMR0 Overflow Interrupt Enable bit
		1 = Enables the TMR0 overflow interrupt
		0 = Disables the TMR0 overflow interrupt
	bit 4 INT0IE: INT0 External Interrupt Enable bit
		1 = Enables the INT0 external interrupt
		0 = Disables the INT0 external interrupt
	bit 3 RBIE: RB Port Change Interrupt Enable bit
		1 = Enables the RB port change interrupt
		0 = Disables the RB port change interrupt
	bit 2 TMR0IF: TMR0 Overflow Interrupt Flag bit
		1 = TMR0 register has overflowed (must be cleared in software)
		0 = TMR0 register did not overflow
	bit 1 INT0IF: INT0 External Interrupt Flag bit
		1 = The INT0 external interrupt occurred (must be cleared in software)
		0 = The INT0 external interrupt did not occur
	bit 0 RBIF: RB Port Change Interrupt Flag bit(1)
		1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)
		0 = None of the RB7:RB4 pins have changed state
	*/
	
	INTCON = 0x20;                //disable global and enable TMR0 interrupt
  	INTCON2 = 0x84;               //TMR0 high priority
  	RCONbits.IPEN = 1;            //enable priority levels
  	TMR0H = 0;                    //clear timer
  	TMR0L = 0;                    //clear timer
  	T0CON = 0x82;                 //set up timer0 - prescaler 1:8
  	INTCONbits.GIEH = 1;          //enable interrupts*/
}

void StartTimer0(void)
{
	T0CON |= (1<<TMR0ON);
	
}


void StopTimer0(void)
{
	T0CON &= !(1<<TMR0ON);
	
}




#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{
	//INTCONbits.TMR0IF = 0; 
  	if ((INTCON & 0x04)== 0x04)
    	{                                   //check for TMR0 overflow
	if(Carga1s == 15)
	{
		GPDout('D',0xff);
		Carga1s--;
		
	}
	if(Carga1s == 30)
	{
		GPDout('D',0x00);
		Carga1s = 30;	
	}
	else Carga1s--;	
    }
}


